﻿using System.Collections.Generic;
using System.Linq;
using KillBill.Business.Entities;
using KillBill.Presentation.Common.WindowsForms.View;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Presentation.Modules.Administration.Presenter;

namespace KillBill.Presentation.Modules.Administration.View
{
    public partial class EmployeeListView : KillBillWinFormView, IEmployeeList
    {
        public EmployeeListPresenter Presenter { get; set; }

        private List<Employee> _employeeList;

        public EmployeeListView()
        {
            InitializeComponent();
            Presenter = new EmployeeListPresenter(this);
        }

        public List<Employee> Employees
        {
            set
            {
                var rowIndex = 0;
                DgvEmployees.Rows.Clear();
                foreach (var employee in value)
                {
                    DgvEmployees.Rows.Add();
                    //Person
                    DgvEmployees[0, rowIndex].Value = employee.Person.PersonID;
                    DgvEmployees[1, rowIndex].Value = employee.Person.FirstName;
                    DgvEmployees[2, rowIndex].Value = employee.Person.LastName;
                    DgvEmployees[3, rowIndex].Value = employee.Person.Email;
                    DgvEmployees[4, rowIndex].Value = employee.Person.Address;
                    DgvEmployees[5, rowIndex].Value = employee.Person.Phone;
                    //Employee
                    DgvEmployees[6, rowIndex].Value = employee.EmployeeRoleID;
                    DgvEmployees[7, rowIndex].Value = (employee.IsActive ? Properties.Resources.PersonActive : Properties.Resources.PersonDelete);             
                    rowIndex++;
                }
                _employeeList = value;
            }
        }

        private void BtnRefresh_Click(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void TxtSearch_TextChanged(object sender, System.EventArgs e)
        {
            Presenter.Find(TxtSearch.Text);
        }

        private void BtnEdit_Click(object sender, System.EventArgs e)
        {
            var employee =
                _employeeList.Where(em => em.EmployeeID == DgvEmployees.SelectedRows[0].Cells[0].Value.ToString()).First();
            var form = new EmployeeAddEditView(employee);
            form.ShowDialog();
        }

        private void BtnCancel_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        private void BtnAdd_Click(object sender, System.EventArgs e)
        {
            var f = new EmployeeAddEditView();
            f.ShowDialog();
        }
    }
}
